package com.example.demo1.service.impl;

import com.example.demo1.mapper.CategoryMapper;
import com.example.demo1.pojo.Category;
import com.example.demo1.service.CategoryService;
import com.example.demo1.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void add(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        Map<String,Object> user = ThreadLocalUtil.get();
        category.setCreateUser((Integer) user.get("id"));
        categoryMapper.add(category);
    }

    @Override
    public List<Category> list() {
        Map<String,Object> user = ThreadLocalUtil.get();
        Integer userId = (Integer) user.get("id");
        return categoryMapper.list(userId);
    }

    @Override
    public Category findById(Integer id) {
        return categoryMapper.findById(id);
    }

    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    @Override
    public void deleteById(Integer[] id) {
        for(int i = 0; i < id.length; i++){
            categoryMapper.deleteById(id[i]);
        }
    }
}
